class TimeCard {
    /**
     * 获取次卡
     * @api {GET}         /              获取次卡
     * @apiName           /timeCard
     * @apiGroup          TimeCard
     * @apiVersion 0.1.0
     * @apiParam          {Int}            page        页数
     * @apiParam          {Int}            limit       条数
     * @apiParam          {String}         keywords    关键字
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": [
            {
                "id": 1,
                "price": "99.00",
                "actualPrice": "89.99",
                "limit": 20,
                "seconds": 1200,
                "effective": 365,
                "desc": "20次，此卡，有效期一年",
                "status": true,
                "createdAt": "2019-06-05T08:51:28.000Z",
                "updatedAt": "2019-06-05T08:51:30.000Z"
            }
        ]
       }
     */
    async index(ctx) {
        const {
            db,
            Op,
            request: {
                query: { page = 1, limit = 10, keywords = '' }
            }
        } = ctx;

        let condition = {
            limit: parseInt(limit),
            offset: (parseInt(page) - 1) * 10,
            where: {
                id: {
                    [Op.ne]: 0
                }
            }
        };

        //如果关键字不为空
        if (keywords.trim()) {
            condition.where = {
                id: {
                    [Op.ne]: 0
                },
                [Op.or]: [
                    { id: { [Op.like]: '%' + keywords.trim() + '%' } },
                    { desc: { [Op.like]: '%' + keywords.trim() + '%' } }
                ]
            };
        }

        let data = await db.TimeCard.findAndCountAll(condition);
        ctx.success(data);
    }
    /**
     * 次卡新增
     * @api {POST}         /              次卡新增
     * @apiName           /timeCard
     * @apiGroup          TimeCard
     * @apiVersion 0.1.0
     * @apiParam          {Number}       price         原价
     * @apiParam          {Number}       actualPrice   实际价格
     * @apiParam          {Number}       effective     有效期
     * @apiParam          {Number}       limit         次数
     * @apiParam          {String}       desc          描述
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async create(ctx) {
        const {
            db,
            request: {
                body: { price, actualPrice, effective, limit, desc }
            }
        } = ctx;

        let condition = {
            where: {
                price: price,
                actualPrice: actualPrice,
                effective: effective,
                desc: desc,
                limit: limit
            },
            defaults: {
                price: price,
                actualPrice: actualPrice,
                effective: effective,
                desc: desc,
                limit: limit
            }
        };

        db.TimeCard.findCreateFind(condition);
        ctx.success();
    }
    /**
     * 次卡编辑
     * @api {PUT}         /:id              次卡编辑
     * @apiName           /timeCard
     * @apiGroup          TimeCard
     * @apiVersion 0.1.0
     * @apiParam          {Number}       price         原价
     * @apiParam          {Number}       actualPrice   实际价格
     * @apiParam          {Number}       effective     有效期
     * @apiParam          {Number}       limit         次数
     * @apiParam          {String}       desc          描述
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async edit(ctx) {
        const {
            db,
            params: { id },
            request: {
                body: { price, actualPrice, effective, limit, desc }
            }
        } = ctx;

        let data = await db.TimeCard.findOne({ where: { id: id } });

        if (data) {
            data.price = price;
            data.actualPrice = actualPrice;
            data.effective = effective;
            data.limit = limit;
            data.desc = desc;
        }
        data.save();
        ctx.success();
    }
    /**
     * 删除
     * @api {DELETE}      /:id              次卡删除
     * @apiName           /timeCard
     * @apiGroup          TimeCard
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": []
       }
     */
    async deletes(ctx) {
        const {
            db,
            params: { id }
        } = ctx;
        // 先查询是否有人购买次卡片
        db.TimeCard.destroy({ where: { id: id } });
        ctx.success();
    }
}
module.exports = new TimeCard();
